'''
Author: 火红色祥云 809880192@qq.com
Date: 2023-12-26 15:35:11
LastEditors: 火红色祥云 809880192@qq.com
LastEditTime: 2023-12-26 16:53:55
FilePath: /project_luckfox/iic/iic.py
Description: 这段代码通过 write_i2c_block_data 方法向所有可能的I2C地址发送数据块。
            通过迭代 range(0x7F) ，尝试向每个I2C地址发送 data 数组中的数据块。
            如果成功发送，会打印出相应的I2C地址（0x00到0x7F），
            如果由于 OSError 异常发送失败，则忽略该异常，继续尝试下一个地址。
'''
import smbus

def main():
    data = [0x01, 0x02]

    try:
        i2c_bus = smbus.SMBus(3)  

        print("i2cdetect addr : ", end="")
        for address in range(0x7F):
            try:
                i2c_bus.write_i2c_block_data(address, 0, data)
                print("0x{:02X},".format(address), end="")
            except OSError:
                pass  

        print()

    except Exception as e:
        print(f"An error occurred: {e}")

    finally:
        if i2c_bus:
            i2c_bus.close()

if __name__ == "__main__":
    main()